<?php
# +--------------------------------------------------------
# |Description: 中直商城对接接口：Access Token处理类
# +--------------------------------------------------------
# |Company: 北京申合信科技发展有限公司
# +--------------------------------------------------------
# |WebSite: www.shenhexin.com
# +--------------------------------------------------------
# |Author: Akon<18363851918@163.com>
# +--------------------------------------------------------
# |Date: 2017年6月29日
# +--------------------------------------------------------

namespace Zapi\Controller;
use Think\Controller;

class Auth2Controller extends Controller {

    /**
     * 获取token
     * URL http://api.shenhexin.com/zapi/auth2/getAccessToken
     * METHOD POST
     */
    public function getAccessToken(){
        $receive = json_decode(file_get_contents("php://input"),true);
        //$receive = $receive['getToken'];
        $user_info = M('members','sdb_','DB_CONFIG2')->where(array('name'=>$receive['account']))->field('api_pass,name')->find();

        if(!$user_info){
            $info = array(
                'error' => '用户名或密码错误'
            );
            exit(json_encode($info,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
        }
        if($receive['key'] != $user_info['api_pass']) {
            $info = array(
                'error' => '用户名或密码错误'
            );
            exit(json_encode($info,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
        }

        //生成Access_token规则：对用户名和密码进行md5加密，连接当前时间戳
        $access_token = generate_token($receive['account'],$receive['key']);
        $info = array(
            "success" => "true",
            "token" => $access_token,
            "expires_at" => strtotime($receive['timestamp']) + 43200
        );
        //更新access_token
        M('members','sdb_','DB_CONFIG2')->where(array('name'=>$receive['account']))->setField(array('access_token'=>$access_token,'create_time'=>time(),'expire_time'=>$info['expires_at']));
        $this->ajaxReturn($info);
    }

}